package com.zlyx.easy.api.config;

import java.text.MessageFormat;
import java.util.List;

import org.apache.maven.model.Developer;
import org.springframework.beans.factory.annotation.Autowired;

import com.zlyx.easy.core.annotations.SpringBean;
import com.zlyx.easy.core.collections.Lists;
import com.zlyx.easy.core.tool.StringFormat;

import io.swagger.models.Contact;
import io.swagger.models.Info;
import io.swagger.models.Swagger;

/**
 * @Auth 赵光
 * @Describle
 */
@SpringBean(todo = { "配置文档" })
public class ApiConfigurer {

	private static ApiProperties apiProperties;

	@Autowired
	public ApiConfigurer(ApiProperties apiProperties) {
		ApiConfigurer.apiProperties = apiProperties;
	}

	private static String mavenDependency = "&lt;dependency&gt;<br/>"
			+ "&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;{0}&lt;/groupId&gt;<br/>"
			+ "&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;{1}&lt;/artifactId&gt;<br/>"
			+ "&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;{2}&lt;/version&gt;<br/>" + "&lt;/dependency&gt;<br/>";

	public static Swagger getSwagger() {
		Swagger swagger = new Swagger();
		Info info = swagger.getInfo();
		if (info == null) {
			info = new Info();
			swagger.setInfo(info);
		}
		info.setTitle(apiProperties.getName());
		info.setDescription(MessageFormat.format(mavenDependency, apiProperties.getGroupId(),
				apiProperties.getArtifactId(), apiProperties.getVersion()));
		info.setVersion(apiProperties.getVersion());
		info.setTermsOfService(apiProperties.getUrl());
		List<String> developers = Lists.newList();
		List<String> emails = Lists.newList();
		if (!apiProperties.getDevelopers().isEmpty()) {
			List<Developer> developerList = apiProperties.getDevelopers();
			for (Developer developer : developerList) {
				developers.add(developer.getName());
				emails.add(developer.getEmail());
			}
		}
		Contact contact = new Contact().name(StringFormat.format(developers, ", "))
				.email(StringFormat.format(emails, ", ")).url(apiProperties.getUrl());
		info.setContact(contact);
//		swagger.setBasePath(apiProperties.getBasePath());
		return swagger;
	}

	public static ApiProperties getApiProperties() {
		return apiProperties;
	}
}
